<?php

namespace app\admin\controller\freight;

use app\admin\model\freight\ExpressDelivery;
use app\common\controller\AdminController;
use app\common\service\freight\CityService;
use app\common\service\freight\ExpressService;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="express_delivery")
 */
class Express extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\freight\ExpressDelivery();
        
        $this->assign('getIsPostageList', $this->model->getIsPostageList());

    }

    public function cityList($pid = 0)
    {
        $data = CityService::getCityList($pid);
        if ($data['code'] == 200){
            return json(['code' => 1,'msg' => null,'data'=>$data['data']]);
        }
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'template_name' => 'require|unique:express_delivery,template_name'
            ];
            $this->validate($post, $rule);
            try {
                $save = ExpressService::expressCreate($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    public function templateList()
    {
        $data = ExpressDelivery::select();
        $data && $data = $data->toArray();
        return json(['code' => 1,'msg'=>null,'data'=>$data]);
    }


}